<?php 
	include('template/header.php');
	include('classess/user_subscribe.php');

	$usr = new User_subscribe();	
	if(isset($_GET['ref_url'])){
	
		$select_query = "SELECT * FROM tbl_affiliate WHERE reference_url='".$_GET['ref_url']."'";
		$mysql_query = mysql_query($select_query) or die(mysql_error());

		$count_rows = mysql_num_rows($mysql_query);
		
		if($count_rows>0){
			while($row = mysql_fetch_array($mysql_query)){
				$_SESSION['reffered_by'] = $row['id']; 
			}
			
			if(isset($_POST['user_name'])){
				$usr_info = $usr->saveUserDetailWithReffrel();		
			}
?>
			<section  class="homepage-slider" id="home-slider">
				<div class="flexslider">
					<ul class="slides">
						<li>
							<img src="img/slider/first_image.jpg" alt="No Image" />					
						</li>
						<li>
							<img src="img/slider/second_image.jpg" alt="No Image" />					
						</li>
					</ul>
				</div>			
			</section>
			<section class="header_text sub">
				<h4><span>Subscribe</span></h4>
			</section>			
			<section class="main-content">				
				<div class="row">
					<div class="span12" style="text-align:center;">					
						<h4 class="title"><span class="text"><strong>SUBSCRIBE</strong> Form</span></h4>
						<?php 
							if(isset($usr_info)){										
						?>
						<div class="alert alert-error">
							<div style="float: right; width: 5%;"><button type="button" class="close" data-dismiss="alert">&times;</button></div>
							<div style="float:left;width: 92%;word-wrap: break-word;"><?php echo $usr_info; ?></div>
							<div style="clear:both;"></div>							
						</div>
						<?php
							}
						?>
						<form action="<?php echo $_SERVER['PHP_SELF']; ?>?ref_url=<?php echo $_GET['ref_url']; ?>" method="POST" name="email_subscribe" >
							<input type="hidden" name="next" value="/">
							<fieldset>
								<div class="control-group">
									<label class="control-label">Username</label>
									<div class="controls">
										<input type="text" placeholder="User Name" name="user_name" id="user_name" class="input-xlarge">
									</div>
								</div>
								<div class="control-group">
									<label class="control-label">Email</label>
									<div class="controls">
										<input type="text" name="user_email" id="user_email"  placeholder="Email" class="input-xlarge">
									</div>
								</div>
								<?php
									$ref_id = '';
									$select_admin_ref ="SELECT * FROM tbl_affiliate where reference_url='".$_GET['ref_url']."'";
									$query_admin_ref = mysql_query($select_admin_ref) or die(mysql_error());

									if(mysql_num_rows($query_admin_ref)>0){
										while($row_admin_ref = mysql_fetch_array($query_admin_ref)){
											$ref_id = 'a_'.$row_admin_ref['id'];
										}
									}
								?>
								<input type="hidden" name="refferal_id" id="refferal_id" value="<?php echo $ref_id; ?>">
								<div class="control-group">
									<button tabindex="3" type="button" class="btn btn-inverse large subscribe_button">Subscribe</button> 
								</div>
							</fieldset>
						</form>	 			
					</div>				
				</div>
			</section>
   <?php
			}
		}
	?>
<?php include('template/footer.php'); ?>
<script>
	$ = jQuery.noConflict();
	$(document).ready(function(){	
		$('.subscribe_button').click(function(){
			if($('#user_name').val().trim() == ''){			
				alert("Please enter a value for the \"User Name\" field.");
				$('#user_name').focus();
				return (false);
			}			
			else if($('#user_email').val().trim() == "")
			{
				alert("Please enter a value for the \"Email\" field.");
				$('#user_email').focus();				
				return (false);
			}if(($('#user_email').val().indexOf("\@") == -1) || ($('#user_email').val().indexOf(".")== -1) || isInvalidateEmail($('#user_email').val()))
			{
				alert("Invalid Email Address");
				$('#user_email').focus();
				return (false);
			}else if($('#refferal_id').val() == ''){
				alert('Something Wrong in this url. Plz Check');
			}else{
				document.email_subscribe.submit();
			}

		});
		
	});

	function isInvalidateEmail(str)
	{
		var atpos=str.indexOf("@");
		var dotpos=str.lastIndexOf(".");
		if (atpos<1 || dotpos<atpos+2 || dotpos+2>=str.length)
		  {
		  return true;
		  }
		return false;
	}

</script>